package Mylinkedlist;


public class myLinkedList {
    static class Listnode {
        private int val;
        private Listnode prev;
        private Listnode next;

        public Listnode(int val) {
            this.val = val;
        }
    }

    public Listnode head;
    public Listnode last;

    public int size() {
        Listnode cur = head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    public void display() {
        Listnode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public boolean contains(int key) {
        Listnode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;

        }
        return false;
    }

    public void addFirst(int data) {
        Listnode node = new Listnode(data);
        if(head==null){
            head=node;
            last=head;
        }
        else{
            node.next=head;
            head.prev=node;
            head=node;
        }
    }
    public void addEnd(int data){
        Listnode node = new Listnode(data);
        if(head==null){
            head=node;
            last=head;
        }
        else{
            last.next=node;
            node.prev=last;
            last=last.next;
        }
    }

    public void addindex(int index,int data){

        checkindex(index);
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()){
            addEnd(data);
            return;
        }
        Listnode node = new Listnode(data);
        Listnode cur=head;
        while (index!=0){
            cur=cur.next;
            index--;
        }
        cur.prev.next=node;
        node.prev=cur.prev;
        cur.prev=node;
        node.next=cur;

    }

    private void checkindex(int index){
        if(index<0||index>size()){
            throw new indexOutOfExcetion(index+" 位置不合法");
        }
    }
    //删除第一个值为key的值
    public void removefirstkey(int key){
        //头结点
        if(head.val==key){
            head=head.next;
            if(head!=null){
                //考虑只有一个节点的时候
                head.prev=null;
            }
            else{
                last=null;
            }
            return;
        }
        Listnode cur = head;
        while (cur.next != null) {
            if(cur.val==key){
                cur.prev.next=cur.next;
                cur.next.prev=cur.prev;
                return;
            }
            cur = cur.next;
        }
        //尾节点
        if(last.val==key){
            last=last.prev;
            last.next=null;
        }

    }
    //删除所有值为key的值
    public void removeallkey(int key){
        Listnode cur = head;
        while (cur != null) {

            if(cur.val==key){
                //头结点
                if(head==cur){
                    head=head.next;
                    if(head!=null){
                        //考虑只有一个节点的时候
                        head.prev=null;
                    }
                    else{
                        last=null;
                    }
                }
                else if(cur==last){
                    last=last.prev;
                    last.next=null;
                }
                else{
                    cur.prev.next=cur.next;
                    cur.next.prev=cur.prev;
                }
            }

            cur = cur.next;
        }


    }
    public void clear(){
        Listnode cur=head;
        while(cur!=null){
            Listnode curNext=cur.next;
            cur.next=null;
            cur.prev=null;
            cur=curNext;
        }
        head=null;
        last=null;
    }

}
